using _ReplaceString_.UI;
using ReLogic.Content;

namespace _ReplaceString_.UI.Elements;

public class ImageElement(Texture2D texture) : UIBase
{
	public bool AutoScale { get; set; } = false;

	public float Scale { get; set; } = 1;

	public Vector2 Anchor { get; set; } = Vector2.Zero;

	public SpriteEffects Effects { get; set; } = SpriteEffects.None;

	public Rectangle? SourceRectange { get; set; } = null;

	public override void DrawSelf(SpriteBatch spriteBatch)
	{
		if (AutoScale)
		{
			spriteBatch.Draw(texture, InnerBox, SourceRectange, ForegroundColor);
			base.DrawSelf(spriteBatch);
			return;
		}

		Vector2 imageSize = SourceRectange == null ?
			texture.Size() :
			new Vector2(SourceRectange.Value.Width, SourceRectange.Value.Height);
		Vector2 size = Scale * imageSize;

		var box = InnerBox;
		float val = (box.Width - size.X) / 2;
		float x = box.Width > size.X ? MathHelper.Lerp(-val, val, (Anchor.X + 1) / 2) : 0;
		val = (box.Height - size.X) / 2;
		float y = box.Height > size.Y ? MathHelper.Lerp(-val, val, (Anchor.Y + 1) / 2) : 0;

		spriteBatch.Draw(texture, box.Center() + new Vector2(x, y), SourceRectange, ForegroundColor, 0, imageSize / 2, Scale, Effects, 0);
		base.DrawSelf(spriteBatch);
	}
}